package com.carpedil.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/** @author Administrator */
@Configuration
public class MqConfig {
  /** 注入工厂（已经预加载） */
  @Resource CachingConnectionFactory connectionFactory;

  /** rabbitmq模板 */
  @Bean(value = "rabbitTemplate")
  public RabbitTemplate rabbitTemplate() {
    return new RabbitTemplate(connectionFactory);
  }

  /**
   * 创建一个队列
   *
   * @return Queue
   */
  @Bean
  public Queue queueOne() {
    return new Queue("one");
  }

  @Bean
  public Queue queueTwo() {
    return new Queue("two");
  }

  @Bean
  public Queue queueThree() {
    return new Queue("three");
  }

  @Bean
  public Queue queueFour() {
    return new Queue("four");
  }
  /***
   * 创建一个普通交换机
   * @return DirectExchange
   */
  @Bean
  public DirectExchange directExchange() {
    return new DirectExchange("directExchange");
  }

  /***
   * 创建一个主题交换机
   * @return DirectExchange
   */
  @Bean
  public TopicExchange topicExchange() {
    return new TopicExchange("topicExchange");
  }

  /***
   * 创建一个扇形交换机
   * @return DirectExchange
   */
  @Bean
  public FanoutExchange fanoutExchange() {
    return new FanoutExchange("fanoutExchange");
  }

  @Bean
  public Binding bindingQueueWithTopicExchange(Queue queueTwo, TopicExchange topicExchange) {
    return BindingBuilder.bind(queueTwo).to(topicExchange).with("j178.#");
  }

  @Bean
  public Binding bindingQueueWithFanoutExchange(Queue queueThree, FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(queueThree).to(fanoutExchange);
  }

  @Bean
  public Binding bindingQueueWithFanoutExchange2(Queue queueFour, FanoutExchange fanoutExchange) {
    return BindingBuilder.bind(queueFour).to(fanoutExchange);
  }
}
